<?php
if (!defined('ZZQSS')){
	die("Access Denied");
}

class CityClass {
	var $db;
	var $table;
	var $tablepre;
	function __construct(){
		//设置全局变量
		global $db,$tablepre;
		$this->db=$db;
		$this->tablepre=$tablepre;
		$this->table=$tablepre."city";
	}
	function CityClass() {
		$this->__construct();
	}
	/**
	 * 添加分类
	 *
	 * @param unknown_type $cityname
	 * @return unknown
	 */
	function add($cityname,$def=0){
		if(!empty($cityname)){
			$this->db->query("insert into {$this->table}(cityname,def) VALUES('{$cityname}',$def)");	
			$this->cachedata();//存储到文件
			return "OK";
		}else return "地区名为空";
	}
	/**
	 * 编辑地区
	 *
	 * @param unknown_type $sid
	 * @param unknown_type $sarray
	 * @return unknown
	 */
	function edit($cid,$cityname){
		if(!empty($cid)&&!empty($cityname)){
				//更新数据库操作
				$sarray=array(
				'cityname'=>$cityname
				);
				$wheresqlarr=" cid='{$cid}'";
				updatetable($this->table,$sarray, $wheresqlarr);
				$this->cachedata();//存储到文件
				return "OK";
		}return "参数错误";
	}
	/**
	 * 删除指定ID分类
	 *
	 * @param unknown_type $typeid
	 * @return unknown
	 */
	function del($cid){
		if(is_numeric($cid))
		{
			$this->db->query("delete from {$this->table} where cid={$cid}");
			$this->cachedata();//存储到文件
			return "OK";
		}else {
			return "参数错误";			
		}
	}
	/**
	 * 返回分类select
	 *
	 * @param unknown_type $var
	 * @param unknown_type $value
	 * @param unknown_type $other
	 * @return unknown
	 */
	function getselect($var="",$value="",$other=""){
		if(cache_read('citydata.php')){
			$arraydata=cache_read('citydata.php');
		}else{
			$arraydata=$this->getdata("","state=1","");
		}
		foreach ($arraydata as $val){
			$arrayoption[$val['cid']]=$val['cityname'];
		}
		return getselectstr($var, $arrayoption, $value, $other);
	}
	/**
	 * 返回分类名称
	 *
	 * @param unknown_type $typeid
	 * @return unknown
	 */
	function getcityname($cid){
		$arraydata=$this->getdata(1,"cid='{$cid}'");
		if(!empty($arraydata)){
			return $arraydata[0]['cityname'];
		}
	}
	
	function getcity(){
		$cityidcookie=get_cookie('cityid');
		$cityid=GetNum(cookie_authcode($cityidcookie,'DECODE'));
		if (empty($cityid)) {
			$cityid=$this->getdef();
		}
		return $cityid;
	}
	function setcity($cid){
		/*
		if (empty($cid)) {
			$cid=$this->getdef();
		}
		*/
		//设置cookie记录地区时间24小时
		$cookietime=time()+(3600*24);
		set_cookie('cityid',cookie_authcode($cid,'ENCODE'),$cookietime);
	}
	/**
	 * 获取默认地区ID
	 *
	 * @return int
	 */
	function getdef(){
		$arraydata=$this->getdata(1,"def=1");
		if(!empty($arraydata)){
			return $arraydata[0]['cid'];
		}else return 0;
	}
	//地区数据缓存
	
	function cachedata(){
		$filename="citydata.php";
		cache_delete($filename);
		$arraydata=$this->getdata("","state=1");
		cache_write($filename, $arraydata);
	}
	
	/**
	 * 获取数据数组
	 *
	 * @param string $limit
	 * @param string $where
	 * @param string $orderby
	 * @return array
	 */
	function getdata($limit="",$where="",$orderby=""){
		$tempdata=array();
		if(!empty($limit))$limit=" limit $limit ";
		if(!empty($where))$where=" where $where ";
		if(!empty($orderby))$orderby=" order by $orderby ";else $orderby=" order by cid desc";
		$sql="select * from {$this->table}{$where}{$orderby}{$limit}";
		$query =$this->db->query($sql);
		while($value = $this->db->fetch_array($query)) {
			$tempdata[]=$value;
		}
		return $tempdata;
	
	}
	function getcount($where=""){
	if(!empty($where))$where=" where $where";
	$count= $this->db->result_first("Select count(*) From `$this->table` $where");
	return $count;
	
	}
	
}
?>